package com.ship.dispatch.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.common.business.service.impl.BaseServiceImpl;
//import com.ship.common.core.base.Page;
import com.ship.common.core.utils.StringUtil;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.SpBoastPortMessage;
import com.ship.dispatch.bean.SpBoatsActualCompatible;
import com.ship.dispatch.bean.boats.QueryBoatsActualRq;
import com.ship.dispatch.bean.boats.info.QueryBoatsActualCompatibleInfo;
import com.ship.dispatch.bean.boats.info.QueryBoatsPortInfo;
import com.ship.dispatch.bean.boats.info.QueryPortInfo;
import com.ship.dispatch.bean.fob.SpFobFileRecord;
import com.ship.dispatch.bo.SpBoatsActualCompatibleBo;
import com.ship.dispatch.mapper.SpBoatsActualCompatibleMapper;
import com.ship.dispatch.model.Vo.SpBoatsActualCompatibleSaveVo;
import com.ship.dispatch.model.dto.BoatsActualCompatibilitySaveDto;
import com.ship.dispatch.model.dto.BoatsActualCompatibilityUpdateDto;
import com.ship.dispatch.model.dto.SpBoastPortMessageDto;
import com.ship.dispatch.service.BoastPortMessageService;
import com.ship.dispatch.service.SpBoastPortMessageService;
import com.ship.dispatch.service.SpBoatsActualCompatibleService;
import com.ship.dispatch.vo.SpBoastPortMessageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SpBoatsActualCompatibleServiceImpl extends BaseServiceImpl<SpBoatsActualCompatibleMapper, SpBoatsActualCompatible>
        implements SpBoatsActualCompatibleService {
    @Autowired
    private BoastPortMessageService boastPortMessageService;

    /**
     * 船运执行同步船岸兼容
     *
     * @param boatsActualCompatibleBo
     * @return
     */
    @Override
    public Boolean synchronShipShoreCompatibility(SpBoatsActualCompatibleBo boatsActualCompatibleBo) {
        SpBoatsActualCompatible spBoatsActualCompatible = new SpBoatsActualCompatible();
        BeanUtils.copyProperties(boatsActualCompatibleBo, spBoatsActualCompatible);
        spBoatsActualCompatible.setCreateBy(SecurityUtils.getStringUserId());
        spBoatsActualCompatible.setCreateDate(new Date());
        spBoatsActualCompatible.setShipShoreCompatibility("1");
        return this.save(spBoatsActualCompatible);
    }

    /**
     * 港口挂靠船舶列表
     *
     * @param boastPortMessageDto
     * @return
     */
    @Override
    public PageInfo<SpBoastPortMessageVo> getBoastPortMessages(SpBoastPortMessageDto boastPortMessageDto) {
        PageHelper.startPage(boastPortMessageDto.getPageNum(),boastPortMessageDto.getPageSize());
        List<SpBoastPortMessageVo> boastPortMessageVos = boastPortMessageService.queryBoatsPortPage(boastPortMessageDto.getPortNameChsList());
        PageInfo<SpBoastPortMessageVo> pageInfo = new PageInfo<>(boastPortMessageVos);
        if (CollectionUtil.isNotEmpty(boastPortMessageVos)){
            boastPortMessageVos.stream().forEach(b->{
                List<QueryPortInfo> portList = boastPortMessageService.countShipTotal(b.getPortNameChs());
                if (CollectionUtil.isNotEmpty(portList)){
                    b.setShipNum(portList.size());
                    if (portList.size()>=5){
                        b.setShipTopOne(portList.get(0).getShipName());
                        b.setShipTopTwo(portList.get(1).getShipName());
                        b.setShipTopThird(portList.get(2).getShipName());
                        b.setShipTopFourth(portList.get(3).getShipName());
                        b.setShipTopFifth(portList.get(4).getShipName());
                    }else {
                        for (int i = 1; i <= portList.size(); i++) {
                            if (i==1){
                                b.setShipTopOne(portList.get(0).getShipName());
                            }else if (i == 2){
                                b.setShipTopTwo(portList.get(1).getPortNameEn());
                            }else if (i == 3){
                                b.setShipTopThird(portList.get(2).getPortNameEn());
                            }else if (i == 4){
                                b.setShipTopFourth(portList.get(3).getPortNameEn());
                            }else if (i == 5){
                                b.setShipTopFifth(portList.get(4).getPortNameEn());
                            }
                        }
                    }
                }
            });
        }
        pageInfo.setList(boastPortMessageVos);
        return pageInfo;
    }

    @Override
    public PageInfo<QueryBoatsActualCompatibleInfo> queryBoatsActualList(QueryBoatsActualRq rq) {
        if (StringUtils.isNotEmpty(rq.getShipName()) || StringUtils.isEmpty(rq.getImo())){
            String imo = baseMapper.queryImoByShipName(rq.getShipName());
            rq.setImo(imo);
        }

        if (StringUtils.isNotEmpty(rq.getSisterStatus()) && "1".equals(rq.getSisterStatus())){
            List<String> imoList = baseMapper.queryBoatsActualImoList(rq);
            //根据查询出来的船舶iom号查询所有船舶的姊妹船imo号
            if (CollectionUtil.isNotEmpty(imoList)){
                List<String> sisterIdList = baseMapper.queryBoatsPortSisterAll(imoList);
                if (CollectionUtil.isNotEmpty(sisterIdList)) {
                    List<String> list2 = sisterIdList.stream().distinct().collect(Collectors.toList());
                    imoList.addAll(list2);

                    PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
                    List<QueryBoatsActualCompatibleInfo> boatsActualCompatibleInfoList = baseMapper.queryBoatsActualListByImo(imoList);
                    PageInfo<QueryBoatsActualCompatibleInfo> pageInfo = new PageInfo<>(boatsActualCompatibleInfoList);
                    return pageInfo;
                }

            }
        }else {
            PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
            List<QueryBoatsActualCompatibleInfo> boatsActualCompatibleInfoList = baseMapper.queryBoatsActualList(rq);
            PageInfo<QueryBoatsActualCompatibleInfo> pageInfo = new PageInfo<>(boatsActualCompatibleInfoList);
            return pageInfo;
        }
        return null;
    }

    /**
     * 新增实际船岸兼容
     * @param boatsActualCompatibilitySaveDto
     */
    @Override
    public SpBoatsActualCompatible saveBoatsActualCompatibility(BoatsActualCompatibilitySaveDto boatsActualCompatibilitySaveDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ship_name",boatsActualCompatibilitySaveDto.getShipName());
        queryWrapper.eq("enable","1");
        queryWrapper.eq("port_name",boatsActualCompatibilitySaveDto.getPortName());
        SpBoatsActualCompatible spBoatsActualCompatible = baseMapper.selectOne(queryWrapper);
        if (null != spBoatsActualCompatible){
            throw new SecurityException("该数据已存在，请查看或编辑船岸兼容信息");
        }
        SpBoatsActualCompatible boatsActualCompatible = new SpBoatsActualCompatible();
        BeanUtils.copyProperties(boatsActualCompatibilitySaveDto,boatsActualCompatible);
        boatsActualCompatible.setCreateDate(new Date());
        boatsActualCompatible.setEnable("1");
        boatsActualCompatible.setCreateBy(SecurityUtils.getStringUserId());
        this.save(boatsActualCompatible);
        return boatsActualCompatible;
    }

    /**
     * 实际船岸兼容详情
     * @param id
     * @return
     */
    @Override
    public SpBoatsActualCompatibleSaveVo getBoatsActualCompatibility(Long id){
        SpBoatsActualCompatibleSaveVo boatsActualCompatibleSaveVo = new SpBoatsActualCompatibleSaveVo();
        SpBoatsActualCompatible boatsActualCompatible = this.getById(id);
        if(null != boatsActualCompatible){
            BeanUtils.copyProperties(boatsActualCompatible,boatsActualCompatibleSaveVo);
        }
        return boatsActualCompatibleSaveVo;
    }

    /**
     * 编辑实际船岸兼容
     * @param dto
     * @return
     */
    @Override
    public SpBoatsActualCompatibleSaveVo updateBoatsActualCompatibility(BoatsActualCompatibilityUpdateDto dto){
        SpBoatsActualCompatible old = this.getById(Long.valueOf(dto.getId()));
        if(old == null){
            throw new SecurityException("数据信息异常");
        }
        if(!StringUtils.equals(dto.getShipName(),old.getShipName()) || !StringUtils.equals(dto.getPortName(),old.getPortName())){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("ship_name",dto.getShipName());
            queryWrapper.eq("enable","1");
            queryWrapper.eq("port_name",dto.getPortName());
            Long count = baseMapper.selectCount(queryWrapper);
            if (count > 0){
                throw new SecurityException("该数据已存在，请查看或编辑船岸兼容信息");
            }
        }
        BeanUtils.copyProperties(dto,old);
        this.updateById(old);
        return new SpBoatsActualCompatibleSaveVo();
    }

    @Override
    public List<QueryBoatsActualCompatibleInfo> exportBoatsActualCompatible(SpBoatsActualCompatible boatsActualCompatible) {
        QueryBoatsActualRq queryBoatsActualRq = new QueryBoatsActualRq();
        queryBoatsActualRq.setImo(boatsActualCompatible.getImo());
        queryBoatsActualRq.setShipName(boatsActualCompatible.getShipName());
        queryBoatsActualRq.setLoadTerminal(boatsActualCompatible.getPortName());
        List<QueryBoatsActualCompatibleInfo> boatsActualCompatibleInfoList = baseMapper.queryBoatsActualList(queryBoatsActualRq);
        return boatsActualCompatibleInfoList;
    }

    @Override
    public boolean deleteBoatsActualCompatibilityById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        int update = baseMapper.update(null, updateWrapper);
        if (update == 1){
            return true;
        }
        return false;
    }
}
